﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Transactions;

namespace Hack.Helpers
{
    [Obsolete("Not sure if gonna use")]
    public static class RegistryHelper
    {
        public enum HKey : uint
        {
            HKEY_CURRENT_USER = 0x80000001
        }
        public enum RegSam : uint
        {
            KEY_WOW64_32KEY = 0x0200,
            KEY_WOW64_64KEY = 0x0100
        }
        [ComImport]
        [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        [Guid("79427A2B-F895-40e0-BE79-B57DC82ED231")]
        internal interface IKernelTransaction
        {
            void GetHandle([Out] out IntPtr handle);
        }
        public static void DeleteKey(HKey hkey, string subkey)
        {
            if (Transaction.Current != null)
            {
                IKernelTransaction tx = (IKernelTransaction)TransactionInterop.GetDtcTransaction(Transaction.Current);
                IntPtr txh;
                tx.GetHandle(out txh);

                if (txh == IntPtr.Zero)
                    throw new Exception(); //Q-n-D

                if (Hack.SystemAPI.NativeDlls.RegDeleteKeyTransacted((uint)hkey, subkey, RegSam.KEY_WOW64_32KEY, 0, txh, IntPtr.Zero) != null)
                    throw new Exception(); //Q-n-D

                Hack.SystemAPI.NativeDlls.CloseHandle(txh);
            }
            else
            {
                //
                // Perform non-transacted delete using Microsoft.Win32 APIs.
                //
            }
        }
    }
}
